/**
 * BrandBigData.com Inc. Copyright (c) 2016 All Rights Reserved.
 */
package com.qibao.toolkit.util;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author xumin
 * @version $Id:PythonUtils.java, v0.1 2017/7/31 11:33 xumin
 */
public abstract class PythonUtils {

    private static Pattern pattern = Pattern.compile("(\\{[^:{\\s]+\\})");
    private static Splitter lineSpliter = Splitter.on("\n").trimResults().omitEmptyStrings();



    public static List<String> extractVariables(String script) {
        if (StringUtils.isEmpty(script)) {
            return new ArrayList<>();
        }
        Iterable<String> lines = Splitter.on("\n").trimResults().omitEmptyStrings().split(script);
        List<String> result = Lists.newArrayList();
        lines.forEach(line -> {
            if (line.startsWith("#")) {
                if (line.substring(1).trim().startsWith("dependency:")) {
                    String dependency = StringUtils.substringAfter(line, "dependency:");
                    result.addAll(Splitter.on(",").trimResults().omitEmptyStrings()
                            .splitToList(dependency));
                }
            }
        });
        //使用正则提取脚本中的变量，变量格式{***}
        Matcher matcher = pattern.matcher(script);
        Set<String> variables = Sets.newHashSet();
        while (matcher.find()) {
            String group = matcher.group();
            variables.add(group);
        }
        variables.forEach(variable -> result.add(StringUtils.substringBetween(variable, "{", "}")));

        return result;
    }


    /**
     * pyhon代码里是否有代码（注释不算作代码）
     */
    public static boolean isHaveCode(String pythonCode) {
        if (StringUtils.isBlank(pythonCode)) {
            return false;
        }
        pythonCode = pythonCode.replaceAll("[\r]", "\n");

        List<String> lineList = lineSpliter.splitToList(pythonCode);

        boolean haveCode = false;
        boolean inComment = false;
        for (String line : lineList) {
            if (StringUtils.isBlank(line)) {
                continue;
            }
            //这里不需要trim,lineSpliter已经trim过了

            if (line.startsWith("#")) {
                continue;
            }

            if (!inComment && line.startsWith("'''")) {
                inComment = true;
                continue;
            }

            if (inComment && line.endsWith("'''")) {
                inComment = false;
                continue;
            }

            if (!inComment && line.startsWith("\"\"\"")) {
                inComment = true;
                continue;
            }

            if (inComment && line.endsWith("\"\"\"")) {
                inComment = false;
                continue;
            }

            if (inComment) {
                continue;
            }

            haveCode = true;
        }

        return haveCode;
    }

}
